<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class General_Model extends CI_Model
{
	public $rules = array(
		'email' => array(
			'field' => 'email', 
			'label' => 'Email', 
			'rules' => 'trim|required|valid_email|xss_clean'
		), 
		'password' => array(
			'field' => 'password', 
			'label' => 'Password', 
			'rules' => 'trim|required'
		)
	);
	public $rules_admin = array(
		'name' => array(
			'field' => 'name', 
			'label' => 'Name', 
			'rules' => 'trim|required|xss_clean'
		), 
		'email' => array(
			'field' => 'email', 
			'label' => 'Email', 
			'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
		), 
		'password' => array(
			'field' => 'password', 
			'label' => 'Password', 
			'rules' => 'trim|matches[password_confirm]'
		),
		'password_confirm' => array(
			'field' => 'password_confirm', 
			'label' => 'Confirm password', 
			'rules' => 'trim|matches[password]'
		),
	);
	public $rules_pages = array(

		'slug' => array(
			'field' => 'slug', 
			'label' => 'Slug', 
			'rules' => 'trim|max_length[255]|callback_unique_slug_check'
		), 
		'meta_title' => array(
			'field' => 'meta_title', 
			'label' => 'Meta Title', 
			'rules' => 'trim'
		), 
		'meta_description' => array(
			'field' => 'meta_description', 
			'label' => 'Meta Description', 
			'rules' => 'trim'
		),
		'meta_keywords' => array(
			'field' => 'meta_keywords', 
			'label' => 'Meta Keywords', 
			'rules' => 'trim'
		),
		'status' => array(
			'field' => 'status', 
			'label' => 'Status', 
			'rules' => 'trim|required'
		),
		'title' => array(
			'field' => 'title', 
			'label' => 'Entry Title', 
			'rules' => 'trim|required|max_length[100]'
		),
		
		'date_added' => array(
			'field' => 'date_added', 
			'label' => 'Date Created', 
			'rules' => 'trim|required'
		),
	);
	public $rules_post = array(

		'slug' => array(
			'field' => 'slug', 
			'label' => 'Slug', 
			'rules' => 'trim|max_length[255]|callback_unique_slug_check'
		), 
		
		'status' => array(
			'field' => 'status', 
			'label' => 'Status', 
			'rules' => 'trim|required'
		),
		'title' => array(
			'field' => 'title', 
			'label' => 'Entry Title', 
			'rules' => 'trim|required|max_length[100]'
		),
		
		'date_added' => array(
			'field' => 'date_added', 
			'label' => 'Date Created', 
			'rules' => 'trim|required'
		),
	);
	public $rules_join = array(

		'firstname' => array(
			'field' => 'firstname', 
			'label' => 'Firstname', 
			'rules' => 'trim|required|max_length[255]'
		), 
		
		
		'email' => array(
			'field' => 'email', 
			'label' => 'Email', 
			'rules' => 'trim|required|valid_email|is_unique[users.email]',
			
		), 
		'captcha'=>array(
			'field'=>'captcha',
			'label'=>'Captcha',
			'rules'=>'required|callback_validate_captcha'
		)
		
		
	);
	public $rules_updatejoin = array(

		'firstname' => array(
			'field' => 'firstname', 
			'label' => 'Firstname', 
			'rules' => 'trim|required|max_length[255]'
		), 
		
		
		'email' => array(
			'field' => 'email', 
			'label' => 'Email', 
			'rules' => 'trim|required|valid_email',
			
		), 
		
		
	);
	public $rules_businesspost = array(

		'business_name' => array(
			'field' => 'business_name', 
			'label' => 'Business Name', 
			'rules' => 'trim|required|max_length[255]'
		), 
		
		'business_category' => array(
			'field' => 'business_category', 
			'label' => 'Business Category', 
			'rules' => 'trim|required'
		),
		
		'address' => array(
			'field' => 'address', 
			'label' => 'Address', 
			'rules' => 'trim|required'
		),
		
		
	);
	
	public $rules_trial = array(

		'business_name' => array(
			'field' => 'business_name', 
			'label' => 'Business Name', 
			'rules' => 'trim|required|max_length[255]'
		), 
		'owner_information' => array(
			'field' => 'owner_information', 
			'label' => 'Owner Information', 
			'rules' => 'trim|required|max_length[255]'
		), 
		'owner_phone' => array(
			'field' => 'owner_phone', 
			'label' => 'Cell Number', 
			'rules' => 'trim|required|numeric'
		), 
		
		'business_type' => array(
			'field' => 'business_type', 
			'label' => 'Business Type', 
			'rules' => 'trim|required'
		),
		
		'owner_address' => array(
			'field' => 'owner_address', 
			'label' => 'Address', 
			'rules' => 'trim|required'
		),
		
		
	);
	
	public $rules_adduser = array(
		'email' => array(
			'field' => 'email', 
			'label' => 'Email', 
			'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
		), 
		'password' => array(
			'field' => 'password', 
			'label' => 'Password', 
			'rules' => 'trim|matches[confirm_password]'
		),
		'confirm_password' => array(
			'field' => 'confirm_password', 
			'label' => 'Confirm password', 
			'rules' => 'trim'
		),
		'first_name' => array(
			'field' => 'firstname', 
			'label' => 'First Name', 
			'rules' => 'trim|required|xss_clean'
		),
		'last_name' => array(
			'field' => 'lastname', 
			'label' => 'Last Name', 
			'rules' => 'trim|required|xss_clean'
		), 
	);
	
	function __construct ()
	{
		parent::__construct();
	}

	function count_row($table,$cond = '')
    {
		$this->db->from($table);
		if($cond != '')
        {
            $this->db->where($cond);
        }
		$count = $this->db->count_all_results();
		return $count;
    }
	function select_val($table,$field = '',$cond = '')
	{
		$this->db->select($field);
        $this->db->from($table);
        if($cond != '')
        {
            $this->db->where($cond);
        }
        
        $res = $this->db->get()->result_array();
		if(!empty($res)){
             return $res[0];
        }
        
	}
	function select($table,$field = '',$cond = '')
    {
        $this->db->select($field);
        $this->db->from($table);
        if($cond != '')
        {
            $this->db->where($cond);
        }
        
        $res = $this->db->get()->result_array();
        return $res;
    }
	function selectmax($table,$max = '',$field='',$cond = '')
    {
		$this->db->select($field);
        $this->db->select_max($max);
        $this->db->from($table);
        if($cond != '')
        {
            $this->db->where($cond);
        }
        
        $res = $this->db->get()->result_array();
        if(!empty($res)){
             return $res[0];
        }
    }
	function selectdistinct($table,$field = '',$cond = '')
    {
		$this->db->select($field);
		$this->db->distinct();
       
        $this->db->from($table);
        if($cond != '')
        {
            $this->db->where($cond);
        }
        
        $res = $this->db->get()->result_array();
        return $res;
    }
	function update($table , $update_field , $cond)
    {
        $this->db->where($cond);
        $status=$this->db->update($table, $update_field);
        //error_log($this->db->last_query());
        
        if($status) 
        return "success";
    }
	function insert($table,$data)
    {
       $this->db->insert($table, $data); 
       return $this->db->insert_id() ;
       //error_log($this->db->last_query());
    }
	function get_jointable($table1,$table2,$field,$cond2,$where='')
    {
        $this->db->select($field);
        $this->db->from($table1);
        $this->db->join($table2,$cond2);
		
		if($where != '')
		{
			$this->db->where($where);
		}
        $res= $this->db->get()->result_array();
		
			return $res;
		
    }
	function get_join2table($table1,$table2,$field,$cond2,$where='')
    {
        $this->db->select($field);
        $this->db->from($table1);
        $this->db->join($table2,$cond2);
		
		if($where != '')
		{
			$this->db->where($where);
		}
        $res= $this->db->get()->result_array();
		$count=count($res);
		if($count==1)
		{
			return $res[0];
		}
		else
		{
			return $res;
		}
    }
	function get_join3table($table1,$table2,$table3,$field,$cond2,$cond3,$where='')
    {
        $this->db->select($field);
        $this->db->from($table1);
        $this->db->join($table2,$cond2);
		$this->db->join($table3,$cond3);
		if($where != '')
		{
			$this->db->where($where);
		}
        return $this->db->get()->result_array();
    }

    function get_val($table , $field , $cond)
    {
        $this->db->select($field);
        $this->db->from($table);
        $this->db->where($cond);
        $res = $this->db->get()->result_array();
        error_log($this->db->last_query());
        if(!empty($res)){
             return $res[0][$field];
        }
        else 
        {
            return 'invalid';
        }
        
    }

    function get_code()
    {
      $custid = $this->db->get('autogenerated_id');
      return $custid->result_array();
    }
    
      
    /*function get_child_member($mid)
    {
      $this->db->select('id,code,m_parent_id,l_child_id,r_child_id');
      $this->db->from('member');
      $this->db->where('m_parent_id = '.$mid);
      $membinfo=$this->db->get()->result_array();
      return $membinfo;
    }*/
    
    function get_child_member($id){
         $this->db->select('id,code,l_child_id,r_child_id,photo,sponsor_id,fname,lname,mname');
         $this->db->from('member');
         $this->db->where('id = '.$id);
         $membinfo=$this->db->get()->result_array();
         return $membinfo[0];
    }
    /*function get_child_member($mid)
    {
      $this->db->select('id,code,m_parent_id,l_child_id,r_child_id');
      $this->db->from('member');
      $this->db->where('id = '.$mid);
      $membinfo=$this->db->get()->result_array();
      return $membinfo;
    }*/
    
    function treedetails($mid)
    {
      $this->db->select();
      $this->db->from('tree');
      $this->db->where('parent_id = '.$mid);
      $treedetails=$this->db->get()->result_array();
      return $treedetails;
    }
    
    function memberdetails()
    {
      $this->db->select();
      $this->db->from('member');
      
      $categories=$this->db->get()->result_array();
      return $categories;
    }
    
    function delete_row($table,$where)
	 {
		//$tables = array($table);
		$this->db->where($where);
		$status=$this->db->delete($table);
		if($status) 
        return "success";
	 }
	 
	function delete_rows($table,$where,$id)
	{	
		
		$this->db->where_in($where, $id);
		$this->db->delete($table);
	}
	function truncate($table)
	{
		$this->db->truncate($table); 
	}
	function update_row($table,$update_field,$where)
	 {
		//$tables = array($table);
		$this->db->where($where);
		$status=$this->db->update($table, $update_field);
		if($status) 
        return "success";
	 }
	 
	function update_rows($table,$update_field,$where,$id)
	{	
		
		$this->db->where_in($where, $id);
		$status=$this->db->update($table, $update_field);
	}
	function select_rows($table,$field,$cond="",$orderby="",$limit="",$offset="",$where="")
	{
		$this->db->select($field);
		$this->db->from($table);
		if($orderby !='')
		{
			$this->db->order_by($cond,$orderby);
		}
		if($where !='')
		{
			$this->db->where($where);
		}
		if($limit != '')
		{
			$this->db->limit($limit,$offset);
		}
		$res = $this->db->get()->result_array();
		
		return $res;
		
	} 
	function select_ord($table,$field,$cond,$orderby,$where="")
	{
		$this->db->select($field);
		$this->db->from($table);
		
		$this->db->order_by($cond,$orderby);
		if($where != '')
		{
			$this->db->where($where);
		}
		$res = $this->db->get()->result_array();
		
		return $res;
	}
	function select_groupby($table,$field,$groupby,$where="")
	{
		$this->db->select($field);
		$this->db->from($table);
		
		$this->db->group_by($groupby);
		if($where!='')
		{
			$this->db->where($where);
		}
		
		$res = $this->db->get()->result_array();
		
		return $res;
	}
	function selectmult_jointableord($table1,$table2,$field,$cond2,$where,$orderbycond='',$orderby='')
    {
        $this->db->select($field);
        $this->db->from($table1);
        $this->db->join($table2,$cond2);
		$this->db->where($where);
		if($orderby!="")
		{
			$this->db->order_by($orderbycond,$orderby);
		}
        $res= $this->db->get()->result_array();
		
		return $res;
		
    }
	function selectmult_jointable($table1,$table2,$field,$cond2,$wherecond,$wherein,$orderbycond='',$orderby='')
    {
        $this->db->select($field);
        $this->db->from($table1);
        $this->db->join($table2,$cond2);
		$this->db->where_in($wherecond,$wherein);
		if($orderby!="")
		{
			$this->db->order_by($orderbycond,$orderby);
		}
        $res= $this->db->get()->result_array();
		
		return $res;
		
    }
	function selectmult_jointablelike($table1,$table2,$field,$cond2,$wherecond,$wherein,$like,$orderbycond='',$orderby='')
    {
        $this->db->select($field);
        $this->db->from($table1);
        $this->db->join($table2,$cond2);
		$this->db->where_in($wherecond,$wherein);
		$this->db->where($like);
		if($orderby!="")
		{
			$this->db->order_by($orderbycond,$orderby);
		}
        $res= $this->db->get()->result_array();
		
		return $res;
		
    }
	function loggedin($user)
	{
		if($user=="user")
		{
			$loggedin=$this->session->userdata('userlogged');
			
		}
		else
		{
			$loggedin=$this->session->userdata('adminlogged');
		}
		return (bool) $loggedin;
	}
	
	function search($table,$field,$name="",$location="",$limit="",$offset="")
	{
		$this->db->select($field);
		$this->db->from($table);
	
		if($name!='')
		{
			$likename="(CONCAT_WS(' ',firstname,lastname,username) LIKE '%$name%')";
			$this->db->where($likename);
		}
		if($location!='')
		{
			$likelocation="(CONCAT_WS(' ',city,state,country,zip) LIKE '%$location%')";
			$this->db->where($likelocation);
		}
		if($limit!="")
		{
			$this->db->limit($limit,$offset);
		}
		$this->db->where("status = 1");
		$res = $this->db->get()->result_array();
		return $res;
	}
	public function totalmember($search = array())
	{
		$this->db->from("alan_member");
		$this->db->where("status = 1");
		if($search['name']!='')
		{
			$name=$search['name'];
			$likename="(CONCAT_WS(' ',firstname,lastname,username) LIKE '%$name%')";
			$this->db->where($likename);
		}
		if($search['location']!='')
		{
			$location=$search['location'];
			$likelocation="(CONCAT_WS(' ',city,state,country,zip) LIKE '%$location%')";
			$this->db->where($likelocation);
		}
		if($search['gender']!='')
		{
			$this->db->like("gender",$search['gender']);
		}
		if($search['agefrom']!='' && $search['ageto']!='' )
		{
			$this->db->where("`age` BETWEEN ". $search['agefrom'] ." and ".$search['ageto'], NULL, FALSE); 
		}
		if($search['height'] != '')
		{
			$this->db->like("height",$search['height']);
		}
		if($search['marital_status'] != '')
		{
			$this->db->like("marital_status",$search['marital_status']);
		}
		if($search['interested_in'] != '')
		{
			$interested_in=explode(',',$search['interested_in']);
			$i=0;
			foreach($interested_in as $interested)
			{
				if($i==0)
				$this->db->like("interested_in",$interested);
				else
				$this->db->or_like("interested_in",$interested);
				$i++;
			}
		}
		if($search['relationship'] != '')
		{
			$this->db->like("relationship",$search['relationship']);
		}
		if($search['looking_meet'] != '')
		{
			$this->db->like("looking_meet",$search['looking_meet']);
		}
		$this->db->order_by('userid','ASC');
		$this->db->limit(1000,0);
		$count = $this->db->count_all_results();
		return $count;
	}
	public function searchmember($search = array())
	{
		$this->db->from("alan_member");
		$this->db->where("status = 1");
		if($search['name']!='')
		{
			$name=$search['name'];
			$likename="(CONCAT_WS(' ',firstname,lastname,username) LIKE '%$name%')";
			$this->db->where($likename);
		}
		if($search['location']!='')
		{
			$location=$search['location'];
			$likelocation="(CONCAT_WS(' ',city,state,country,zip) LIKE '%$location%')";
			$this->db->where($likelocation);
		}
		if($search['gender']!='')
		{
			$this->db->where("gender",$search['gender']);
		}
		if($search['agefrom']!='' && $search['ageto']!='' )
		{
			$this->db->where("(`age` BETWEEN ". $search['agefrom'] ." and ".$search['ageto'].")", NULL, FALSE); 
		}
		if($search['height'] != '')
		{
			$this->db->like("height",$search['height']);
		}
		if($search['marital_status'] != '')
		{
			$this->db->like("marital_status",$search['marital_status']);
		}
		if($search['interested_in'] != '')
		{
			$interested_in=explode(',',$search['interested_in']);
			$i=0;
			foreach($interested_in as $interested)
			{
				if($i==0)
				$this->db->like("interested_in",$interested);
				else
				$this->db->or_like("interested_in",$interested);
				$i++;
			}
		}
		if($search['relationship'] != '')
		{
			$this->db->like("relationship",$search['relationship']);
		}
		if($search['looking_meet'] != '')
		{
			$this->db->like("looking_meet",$search['looking_meet']);
		}
		
		if($search['sort'] != '')
		{
			if($search['sort']=="name" && $search['order']=="ASC")
			{
				$this->db->order_by('firstname','ASC');
			}
			else if($search['sort']=="name" && $search['order']=="DESC")
			{
				$this->db->order_by('firstname','DESC');
			}
			else if($search['sort']=="age")
			{
				$this->db->order_by('age','ASC');
			}
		}
		else
		{
			$this->db->order_by('userid','ASC');
		}
		$this->db->limit($search['perpage'],$search['offset']);
		$result = $this->db->get()->result_array();
		return $result;
	}
	function validatelogin()
	{
		$this->db->where('email', $this->input->post('email'));
		$this->db->where('password', $this->input->post('password'));
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			$loggedin=array("logged_in"=>1);
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$this->input->post('email')."'".' and password = '."'".$this->input->post('password')."'");
			$userdetails=$this->general_model->select_val('users','*','status = 1 and email = '."'".$this->input->post('email')."'".' and password = '."'".$this->input->post('password')."'");
			$user_type =$userdetails['group_id'];
			$this->session->set_userdata('user_type',$user_type);
			$this->session->set_userdata("user",$userdetails);
		
			return true;
		}
		
	}
	
	
}
?>
